package cn.kgc.itrip.controller;


import cn.kgc.itrip.common.DtoUtil;
import cn.kgc.itrip.common.ErrorCode;
import cn.kgc.itrip.dto.Dto;
import cn.kgc.itrip.dto.ItripUserVO;
import cn.kgc.itrip.service.impl.UserEmailService;
import cn.kgc.itrip.service.impl.UserPhoneService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
public class UserEmailController {


    @Resource
    private UserEmailService userEmailService;

    @Resource
    private UserPhoneService userPhoneService;

    /**
     * 邮箱注册
     * @param itripUserVO
     * @return
     */
    @PostMapping(value = "/doregister",consumes = "application/json",produces = "application/json")
    public Dto doRegister(@RequestBody ItripUserVO itripUserVO){
        Dto dto=null;
        try {
            dto=userEmailService.itripDoRegister(itripUserVO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }


    /**
     * 邮箱激活
     * @param userCode 邮箱地址(redis中的key)
     * @param code 激活码
     * @return
     */
    @PutMapping(value = "/activate",consumes ="application/json",produces = "application/json")
    public Dto activate(@RequestParam("user") String userCode,
                        @RequestParam("code") String code){
        Dto dto=null;
        dto=userEmailService.activateEmail(userCode,code);
        return dto;
    }


    /**
     * 手机注册
     * @param itripUserVO
     * @return
     */
    @PostMapping(value = "/registerbyphone",consumes = "application/json",produces = "application/json")
    public  Dto registerbyphone(@RequestBody ItripUserVO itripUserVO){
        Dto dto=null;
        try {
            dto=userPhoneService.doLoginPhone(itripUserVO);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("该账户已存在", ErrorCode.AUTH_AUTHENTICATION_FAILED);
        }
        return dto;
    }


    /**
     * 手机激活
     * @param user
     * @param code
     * @return
     */
    @PutMapping(value = "/validatephone",consumes = "application/json",produces = "application/json")
    public Dto validatephone(@RequestParam String user,
                             @RequestParam String code){
        Dto dto=null;
        dto=userEmailService.activateEmail(user,code);
        return dto;
    }
}
